package com.firefly.mobile.app.util;

import java.io.File;
import java.io.FileInputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.List;

import com.firefly.mobile.app.context.SytApplication;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.util.Log;

public class AndroidSystemUtil {
	
	
	private static String getLocalIpAddress() {
        try {
			WifiManager wifiManager = (WifiManager) SytApplication.context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			// 获取32位整型IP地址
			int ipAddress = wifiInfo.getIpAddress();
			
			//返回整型地址转换成“*.*.*.*”地址
			return String.format("%d.%d.%d.%d",
			        (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
			        (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
	
	public static String getIpAddress() {
		String ip = getLocalIpAddress();
		if (ip != null) {
			return ip;
		}
		try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface
	                .getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf
	                    .getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()
	                        && inetAddress instanceof Inet4Address) {
	                    // if (!inetAddress.isLoopbackAddress() && inetAddress
	                    // instanceof Inet6Address) {
	                    return inetAddress.getHostAddress().toString();
	                }
	            }
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	public static String getDeviceId(Context context){
		String deviceId = Secure.getString( 
				context.getContentResolver(), Secure.ANDROID_ID);
		return deviceId;
	}
	
	/**
	* isBackground判断程序是否处于后台
	* @param context
	* @return
	*/
	public static boolean isBackground(Context context) {

		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(context.getPackageName())) {
				if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
					SytLog.i("AndroidSystemUtil", String.format("Background App:",appProcess.processName));
					return true;
				} else {
					SytLog.i("AndroidSystemUtil", String.format("Foreground App:",appProcess.processName));
					return false;
				}
			}
		}
		return false;
	}
	
	public static String getLocalMacAddress() {
		String Mac=null;
		try{
			
			String path="sys/class/net/wlan0/address";
			if((new File(path)).exists())
			{
	        	FileInputStream fis = new FileInputStream(path);
				byte[] buffer = new byte[8192];
		        int byteCount = fis.read(buffer);
		        if(byteCount>0)
		        {
		        	Mac = new String(buffer, 0, byteCount, "utf-8");
		        }
			}
	        if(Mac==null||Mac.length()==0)
	        {
	        	path="sys/class/net/eth0/address";
				FileInputStream fis_name = new FileInputStream(path);
				byte[] buffer_name = new byte[8192];
		        int byteCount_name = fis_name.read(buffer_name);
		        if(byteCount_name>0)
		        {
		        	Mac = new String(buffer_name, 0, byteCount_name, "utf-8");
		        }
	        }
	        SytLog.i("AndroidSystemUtil", "获取mac地址："+Mac);
	        
	        if(Mac.length()==0||Mac==null){
	        	return "";
	        }
		}catch(Exception io){
			Log.v("daming.zou**exception*", ""+io.toString());
		}finally{
			
		}
		return Mac.trim();
	}
}
